<% include ../header %>
<h2>用户列表</h2>
<table class="table table-striped">
  <thead class="hd-row">
  <tr>
    <th>昵称</th>
    <th>邮箱</th>
    <th>余额</th>
    <th>注册时间</th>
    <th>权限</th>
    <th>账户余额</th>
    <th>删除</th>
  </tr>
  </thead>
  <tbody>
  <% users.forEach(function(user){ %>
  <tr id="<%= user._id %>">
    <td><%= user.name %></td>
    <td><%= user.email %></td>
    <td><%= parseFloat(user.balance || 0.0).toFixed(1) %></td>
    <td>
      <%= user.reg_time %>
    </td>
    <td>
      <% if(user.isAdmin) {%>
      <a class="btn btn-warning btn-small Admin">撤销超级管理</a>
      <% }else{ %>
      <a class="btn btn-primary btn-small Admin">授予超级管理</a>
      <% } %>
      <% if(user.canOperateShop) {%>
      <a class="btn btn-warning btn-small Shoper">撤销店铺管理</a>
      <% }else{%>
      <a class="btn btn-primary btn-small Shoper">授予店铺管理</a>
      <% } %>
    </td>
    <td>
      <a href="/admin/user/orders?user_id=<%=user._id%>" class="btn btn-primary btn-small" >查看订单</a>
      <a href="/admin/user/balance?user_id=<%=user._id%>" class="btn btn-primary btn-small" >变动记录</a>
      <a href="/admin/user/add_balance?user_id=<%=user._id%>" class="btn btn-primary btn-small" >充值</a>
    </td>
    <td>
      <a class="btn btn-danger btn-small" id="deleteUser" >删除</a>
    </td>
  </tr>
  <% }) %>
  </tbody>
</table>
<!-- Modal -->
<div id="myModal" class="modal hide fade" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
  <div class="modal-header">
    <button type="button" class="close cancel" data-dismiss="modal" aria-hidden="true">×</button>
    <h3 id="myModalLabel">Modal header</h3>
  </div>
  <div class="modal-body">
    <p>确定删除此用户？</p>
  </div>
  <div class="modal-footer">
    <button class="btn btn-primary sure">确定</button>
    <button class="btn btn-danger cancel" data-dismiss="modal" aria-hidden="true">取消</button>
  </div>
</div>
<script type="text/javascript">
  $(function(){
    //监听超级管理员按钮
    $(".Admin").live('click',function(){
      var id = $(this).closest("tr").attr("id");
      var that = $(this);
      $.ajax({
        url: '/admin/user/isAdmin/' + id,
        type: 'GET',
        data: { timeStamp:new Date().getTime() },//解决ie不能兼容问题
        error: function(){
          alert('网络错误，请联系管理员');
        },
        success: function(data){
          if(data){
            that.prev().text("true");
            that.text("撤销")
          }else{
            that.prev().text("false");
            that.text("授予");
          }
        }
      })
    })
    //监听店铺管理员按钮
    $(".Shoper").live('click',function(){
      var id = $(this).closest("tr").attr("id");
      var that = $(this);
      $.ajax({
        url: '/admin/user/canOperateShop/' + id,
        type: 'GET',
        data: {timeStamp:new Date().getTime()},//解决ie不能兼容问题
        error: function(){
          alert('网络错误，请联系管理员');
        },
        success: function(data){
          if(data){
            that.prev().text("true");
            that.text("撤销")
          }else{
            that.prev().text("false");
            that.text("授予");
          }
        }
      })
    });
    //监听删除用户的按钮
    $("#deleteUser").live('click', function(){
      var id = $(this).closest("tr").attr("id");
      var that = $(this);
      //弹出选择是否删除窗口
      $('#myModal').modal({
        keyboard: true,
        show: true,
        backdrop: true
      });
      //管理员取消
      $(".cancel").live('click',function(){
        return ;
      })
      //管理员确定则执行删除操作
      $(".sure").live('click',function(){
        $("#myModal").hide();
        $.ajax({
          url: '/admin/user/delete/' + id,
          type: 'GET',
          data: {timeStamp:new Date().getTime()},
          error: function(){
            alert('网络错误，请联系管理员');
          },
          success: function(data){
            that.closest("tr").remove();
          }
        })
      })
    })

  })
</script>
<% include ../footer %>